package com.ford.trade.processor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ford.trade.entity.AoKeOdds3;
import com.ford.trade.service.AoKeGameService;
import com.ford.trade.service.Odds3Service;
import lombok.Data;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: ford
 * @date: 2022/4/23 13:41
 */
@Data
@Component
public class AoKeOdds3Processor implements PageProcessor {

    private AoKeGameService gameService;
    private Odds3Service oddsService;

    private String type;
    private String date;
    private String cookie;
    private String agent;
    private String indexId;

    @Override
    public void process(Page page) {
        List<AoKeOdds3> byIndexId = oddsService.findByIndexId(Long.valueOf(Integer.valueOf(indexId)));
        Html html = page.getHtml();
        String script = html.css("script").get();
        String dataStr = script.substring(script.indexOf("data_str") + 12);
        String substring = dataStr.substring(0, dataStr.indexOf(";") - 1);
        List<JSONObject> jsonObjects = JSON.parseArray(substring, JSONObject.class);
        List<AoKeOdds3> aoKeOdds3s = new ArrayList<>();
        for (JSONObject jsonObject : jsonObjects) {
            AoKeOdds3 aoKeOdds = new AoKeOdds3();
            if (!CollectionUtils.isEmpty(byIndexId)) {
                aoKeOdds.setId(byIndexId.get(0).getId());
            }
            String companyName = jsonObject.getString("CompanyName");
            aoKeOdds.setType(companyName);

            aoKeOdds.setIndexId(Long.valueOf(indexId));
            aoKeOdds.setDatetime(new Date());
            JSONObject start = jsonObject.getJSONObject("Start");
            JSONObject end = jsonObject.getJSONObject("End");
            aoKeOdds.setSheng(start.getDouble("home"));
            aoKeOdds.setPing(start.getDouble("draw"));
            aoKeOdds.setFu(start.getDouble("away"));
            aoKeOdds.setShengChange(end.getDouble("home"));
            aoKeOdds.setPingChange(end.getDouble("draw"));
            aoKeOdds.setFuChange(end.getDouble("away"));
            aoKeOdds3s.add(aoKeOdds);
        }
        oddsService.saveBatch(aoKeOdds3s);

    }

    private Site site = Site.me()
            .setCharset("gb2312")
            .addHeader("x-requested-with", "XMLHttpRequest")
            .addHeader("sec-fetch-site", "same-origin")
            .addHeader("sec-fetch-mode", "cors")
            .addHeader("sec-fetch-dest", "empty")
            .addHeader("sec-ch-ua-platform", "Windows")
            .addHeader("sec-ch-ua-mobile", "?0")
            .addHeader("sec-ch-ua", "\"Chromium\";v=\"104\", \" Not A;Brand\";v=\"99\", \"Google Chrome\";v=\"104\"")
            .addHeader("referer", "https://www.okooo.com/soccer/match/" + indexId + "/odds/")
            .setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36")
            .setTimeOut(1000)
            .addCookie("cookie", "LastUrl=; FirstURL=www.okooo.com/; FirstOKURL=https%3A//jingcai.okooo.com/; First_Source=www.okooo.com; M_UserName=%22%5Cu98de%5Cu9e1f%5Cu96f7%5Cu9e23%22; M_UserID=31747680; M_Ukey=39755d37eb1ef335ded3a5b5f523199c; data_start_isShow=1; __utmz=56961525." + 1661319270 + ".13.5.utmcsr=baidu|utmccn=(organic)|utmcmd=organic; zq_companytype_odds=MyOddsBooks2; __utmc=56961525; Hm_lvt_213d524a1d07274f17dfa17b79db318f=" + (System.currentTimeMillis() - 588108 - 511906 - 442681) + "," + (System.currentTimeMillis() - 588108 - 511906 - 442681 - 588108) + "," + (System.currentTimeMillis() - 588108 - 511906) + "," + (System.currentTimeMillis() - 588108) + "; pm=; PHPSESSID=2839acf266ece5c71f4b6eca3d32655b9b94d38b; IMUserID=31747680; IMUserName=%E9%A3%9E%E9%B8%9F%E9%9B%B7%E9%B8%A3; OKSID=2839acf266ece5c71f4b6eca3d32655b9b94d38b; OkAutoUuid=3c32b71b3dd64bb12d46361e5a141417; OkMsIndex=3; __utma=56961525.1953077866.1659002210.1662359132.1662361803.24; acw_tc=2f61f27416623637233844951e57db43c5f146e29bea9316ff168d1faccbf3; Hm_lpvt_213d524a1d07274f17dfa17b79db318f=" + System.currentTimeMillis() / 1000 + "; __utmb=56961525.55.9." + System.currentTimeMillis())
            .setSleepTime(3)
            .setRetrySleepTime(1000)
            .setRetryTimes(1);

    @Override
    public Site getSite() {
        return site;
    }
}
